#include "Storage_Adapter.h"
#include <stdio.h>
#include <string.h>

/**
  * @brief  存储系统初始化
  * @param  None
  * @retval Storage_Status_TypeDef 操作状态
  */
Storage_Status_TypeDef Storage_Init(void)
{
#if USE_W25Q64_STORAGE
    W25Q64_Storage_Status_TypeDef status = W25Q64_Storage_Init();
    switch(status)
    {
        case W25Q64_STORAGE_SUCCESS: return STORAGE_SUCCESS;
        case W25Q64_STORAGE_ERROR: return STORAGE_ERROR;
        case W25Q64_STORAGE_CHIP_ERROR: return STORAGE_ERROR;
        default: return STORAGE_ERROR;
    }
#else
    Flash_Status_TypeDef status = Flash_Init();
    switch(status)
    {
        case FLASH_OP_SUCCESS: return STORAGE_SUCCESS;
        case FLASH_OP_ERROR: return STORAGE_ERROR;
        default: return STORAGE_ERROR;
    }
#endif
}

/**
  * @brief  存储系统测试 (已简化，直接返回成功)
  * @param  None
  * @retval Storage_Status_TypeDef 操作状态
  */
Storage_Status_TypeDef Storage_Test(void)
{
    // 简化系统，不进行复杂测试，直接返回成功
    return STORAGE_SUCCESS;
}

/**
  * @brief  保存DHT11数据
  * @param  dht11_data: DHT11数据指针
  * @retval Storage_Status_TypeDef 操作状态
  */
Storage_Status_TypeDef DHT11_Storage_SaveData(DHT11_Data_TypeDef *dht11_data)
{
#if USE_W25Q64_STORAGE
    W25Q64_Storage_Status_TypeDef status = DHT11_W25Q64_SaveData(dht11_data);
    switch(status)
    {
        case W25Q64_STORAGE_SUCCESS: return STORAGE_SUCCESS;
        case W25Q64_STORAGE_ERROR: return STORAGE_ERROR;
        case W25Q64_STORAGE_FULL: return STORAGE_FULL;
        case W25Q64_STORAGE_INVALID_PARAM: return STORAGE_INVALID_PARAM;
        default: return STORAGE_ERROR;
    }
#else
    Flash_Status_TypeDef status = DHT11_Flash_SaveData(dht11_data, timestamp);
    switch(status)
    {
        case FLASH_OP_SUCCESS: return STORAGE_SUCCESS;
        case FLASH_OP_ERROR: return STORAGE_ERROR;
        case FLASH_OP_FULL: return STORAGE_FULL;
        default: return STORAGE_ERROR;
    }
#endif
}

/**
  * @brief  读取最新的DHT11数据
  * @param  data: 读取数据指针
  * @retval Storage_Status_TypeDef 操作状态
  */
Storage_Status_TypeDef DHT11_Storage_ReadLatestData(DHT11_Storage_Data_TypeDef *data)
{
    if(data == NULL)
    {
        return STORAGE_INVALID_PARAM;
    }
    
#if USE_W25Q64_STORAGE
    DHT11_W25Q64_Data_TypeDef w25q64_data;
    W25Q64_Storage_Status_TypeDef status = DHT11_W25Q64_ReadLatestData(&w25q64_data);
    
    if(status == W25Q64_STORAGE_SUCCESS)
    {
        data->humi_int = w25q64_data.humi_int;
        data->humi_deci = w25q64_data.humi_deci;
        data->temp_int = w25q64_data.temp_int;
        data->temp_deci = w25q64_data.temp_deci;
        data->check_sum = w25q64_data.check_sum;
        data->valid_flag = w25q64_data.valid_flag;
        data->reserved[0] = w25q64_data.reserved[0];
        return STORAGE_SUCCESS;
    }
    
    switch(status)
    {
        case W25Q64_STORAGE_ERROR: return STORAGE_ERROR;
        case W25Q64_STORAGE_EMPTY: return STORAGE_EMPTY;
        case W25Q64_STORAGE_INVALID_PARAM: return STORAGE_INVALID_PARAM;
        default: return STORAGE_ERROR;
    }
#else
    DHT11_Flash_Data_TypeDef flash_data;
    Flash_Status_TypeDef status = DHT11_Flash_ReadLatestData(&flash_data);
    
    if(status == FLASH_OP_SUCCESS)
    {
        // 转换数据格式
        data->humi_int = flash_data.humi_int;
        data->humi_deci = flash_data.humi_deci;
        data->temp_int = flash_data.temp_int;
        data->temp_deci = flash_data.temp_deci;
        data->check_sum = flash_data.check_sum;
        data->valid_flag = flash_data.valid_flag;
        data->reserved[0] = flash_data.reserved[0];
        return STORAGE_SUCCESS;
    }
    
    switch(status)
    {
        case FLASH_OP_ERROR: return STORAGE_ERROR;
        default: return STORAGE_ERROR;
    }
#endif
}

/**
  * @brief  擦除所有DHT11数据
  * @param  None
  * @retval Storage_Status_TypeDef 操作状态
  */
Storage_Status_TypeDef DHT11_Storage_EraseAllData(void)
{
#if USE_W25Q64_STORAGE
    W25Q64_Storage_Status_TypeDef status = DHT11_W25Q64_EraseAllData();
    switch(status)
    {
        case W25Q64_STORAGE_SUCCESS: return STORAGE_SUCCESS;
        case W25Q64_STORAGE_ERROR: return STORAGE_ERROR;
        default: return STORAGE_ERROR;
    }
#else
    Flash_Status_TypeDef status = DHT11_Flash_EraseData();
    switch(status)
    {
        case FLASH_OP_SUCCESS: return STORAGE_SUCCESS;
        case FLASH_OP_ERROR: return STORAGE_ERROR;
        default: return STORAGE_ERROR;
    }
#endif
}

/**
  * @brief  检查是否有有效数据
  * @param  None
  * @retval uint8_t 1=有有效数据, 0=无有效数据
  */
uint8_t DHT11_Storage_HasValidData(void)
{
#if USE_W25Q64_STORAGE
    return DHT11_W25Q64_HasValidData();
#else
    return DHT11_Flash_HasValidData();
#endif
}

/**
  * @brief  获取记录总数
  * @param  None
  * @retval uint32_t 记录总数
  */
uint32_t DHT11_Storage_GetRecordCount(void)
{
#if USE_W25Q64_STORAGE
    return DHT11_W25Q64_GetRecordCount();
#else
    // 内部Flash只保存一条记录
    return DHT11_Flash_HasValidData() ? 1 : 0;
#endif
}

/**
  * @brief  打印最新的DHT11数据
  * @param  None
  * @retval None
  */
void DHT11_Storage_PrintLatestData(void)
{
    DHT11_Storage_Data_TypeDef data;
    Storage_Status_TypeDef status;
    
#if USE_W25Q64_STORAGE
    printf("\r\n=== W25Q64中存储的最新DHT11数据 ===\r\n");
#else
    printf("\r\n=== Flash中存储的最新DHT11数据 ===\r\n");
#endif
    
    if(DHT11_Storage_HasValidData())
    {
        status = DHT11_Storage_ReadLatestData(&data);
        if(status == STORAGE_SUCCESS)
        {
            printf("温度: %d.%d℃\r\n", data.temp_int, data.temp_deci);
            printf("湿度: %d.%d%%RH\r\n", data.humi_int, data.humi_deci);
            printf("校验和: 0x%02X\r\n", data.check_sum);
            printf("数据状态: %s\r\n", (data.valid_flag == 0xAA) ? "有效" : "无效");
            
#if USE_W25Q64_STORAGE
            printf("总记录数: %d\r\n", DHT11_Storage_GetRecordCount());
            printf("存储模式: %s\r\n", 
                   (DHT11_Storage_GetMode() == 0) ? "覆盖模式" : "环形缓冲模式");
#endif
        }
        else
        {
            printf("读取数据失败，错误码: %d\r\n", status);
        }
    }
    else
    {
        printf("暂无有效数据\r\n");
    }
    printf("================================\r\n");
}

#if USE_W25Q64_STORAGE
/**
  * @brief  根据索引读取DHT11数据 (仅W25Q64支持)
  * @param  index: 数据索引
  * @param  data: 读取数据指针
  * @retval Storage_Status_TypeDef 操作状态
  */
Storage_Status_TypeDef DHT11_Storage_ReadDataByIndex(uint32_t index, DHT11_Storage_Data_TypeDef *data)
{
    if(data == NULL)
    {
        return STORAGE_INVALID_PARAM;
    }
    
    DHT11_W25Q64_Data_TypeDef w25q64_data;
    W25Q64_Storage_Status_TypeDef status = DHT11_W25Q64_ReadDataByIndex(index, &w25q64_data);
    
    if(status == W25Q64_STORAGE_SUCCESS)
    {
        // 转换数据格式
        data->humi_int = w25q64_data.humi_int;
        data->humi_deci = w25q64_data.humi_deci;
        data->temp_int = w25q64_data.temp_int;
        data->temp_deci = w25q64_data.temp_deci;
        data->check_sum = w25q64_data.check_sum;
        data->valid_flag = w25q64_data.valid_flag;
        data->reserved[0] = w25q64_data.reserved[0];
        return STORAGE_SUCCESS;
    }
    
    switch(status)
    {
        case W25Q64_STORAGE_ERROR: return STORAGE_ERROR;
        case W25Q64_STORAGE_EMPTY: return STORAGE_EMPTY;
        case W25Q64_STORAGE_INVALID_PARAM: return STORAGE_INVALID_PARAM;
        default: return STORAGE_ERROR;
    }
}

/**
  * @brief  设置存储模式 (仅W25Q64支持)
  * @param  mode: 存储模式 0=覆盖模式, 1=环形缓冲模式
  * @retval Storage_Status_TypeDef 操作状态
  */
Storage_Status_TypeDef DHT11_Storage_SetMode(uint8_t mode)
{
    W25Q64_Storage_Status_TypeDef status = W25Q64_Storage_SetMode((Storage_Mode_TypeDef)mode);
    switch(status)
    {
        case W25Q64_STORAGE_SUCCESS: return STORAGE_SUCCESS;
        case W25Q64_STORAGE_ERROR: return STORAGE_ERROR;
        default: return STORAGE_ERROR;
    }
}

/**
  * @brief  获取存储模式 (仅W25Q64支持)
  * @param  None
  * @retval uint8_t 存储模式 0=覆盖模式, 1=环形缓冲模式
  */
uint8_t DHT11_Storage_GetMode(void)
{
    return (uint8_t)W25Q64_Storage_GetMode();
}

/**
  * @brief  打印所有DHT11数据 (仅W25Q64支持)
  * @param  None
  * @retval None
  */
void DHT11_Storage_PrintAllData(void)
{
    DHT11_W25Q64_PrintAllData();
}

/**
  * @brief  打印存储系统信息 (仅W25Q64支持)
  * @param  None
  * @retval None
  */
void DHT11_Storage_PrintInfo(void)
{
    W25Q64_Storage_PrintInfo();
}

/**
  * @brief  格式化存储区域 (仅W25Q64支持)
  * @param  None
  * @retval Storage_Status_TypeDef 操作状态
  */
Storage_Status_TypeDef DHT11_Storage_Format(void)
{
    W25Q64_Storage_Status_TypeDef status = W25Q64_Storage_Format();
    switch(status)
    {
        case W25Q64_STORAGE_SUCCESS: return STORAGE_SUCCESS;
        case W25Q64_STORAGE_ERROR: return STORAGE_ERROR;
        default: return STORAGE_ERROR;
    }
}
#endif
